package com.supermap.dataservice.project.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.mysql.config.TargetDataSource;
import com.supermap.dataservice.project.mapper.AgrCollect;
import com.supermap.dataservice.project.mapper.AgrCollectMapper;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.product.AgrCollectService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description 农情收藏实现类
 * @date 2018年11月01 上午 11:36
 * @email 1301584033@qq.com
 */
@Service
@TargetDataSource("db2")
public class AgrCollectServiceImpl  implements AgrCollectService {

    @Resource
    private AgrCollectMapper agrCollectMapper;
    @Override
    public JSONUtil putAgrCollect(Integer userId, Integer agrId) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Integer count= agrCollectMapper.putAgrCollect(userId,agrId);
        if(count>0){
            return  new JSONUtil().setData(true).setMsg("收藏成功!");
        }
        return  new JSONUtil().setMsg("收藏失败").setCode(100).setData(false);
    }

    @Override
    public JSONUtil isBindUserAgrInfo(Integer userId, Integer agrId) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
       Integer count= agrCollectMapper.isBindUserAgrInfo(userId,agrId);
       boolean bool=true;
       if(count>0){
           return  new JSONUtil().setData(bool);
       }
        return  new JSONUtil().setMsg("操作失败").setCode(100).setData(bool=false);
    }

    @Override
    public JSONUtil getPageAgrListByUserId(String farmId,String areaCode,String userId, int pageSize, int pageNum,int type,String beginTime,String endTime) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        pageSize =(pageSize-1)*pageNum;
        List<AgrCollect>  collectList=null;
        List<Map<String,Object>> mapList=null;
        JSONUtil jsonUtil = new JSONUtil();
                if(type==1){
                    //查询所有的农情
                    collectList= agrCollectMapper.getAllListAgrInfo(farmId,areaCode,userId,pageSize,pageNum, beginTime,endTime);
                    jsonUtil.setCountSum(agrCollectMapper.getAllListAgrInfoCountSum(areaCode,userId));
                   if(!collectList.isEmpty()&&collectList!=null){
                       for (AgrCollect agrCollect:collectList){
                           mapList=agrCollectMapper.getListInteract(agrCollect.getId()+"");
                           if(mapList.size()>0){
                               agrCollect.setMapList(mapList);
                           }
                       }
                   }
                }else if(type==2){
                    //自己的农情
                    collectList= agrCollectMapper.getMySelfAllListAgrInfo(userId,pageSize,pageNum);
                    jsonUtil.setCountSum(agrCollectMapper.getMySelfAllListAgrInfoCountSum(userId));
                    for (AgrCollect agrCollect:collectList){
                        mapList=agrCollectMapper.getListInteract(agrCollect.getId()+"");
                        if(mapList.size()>0){
                            agrCollect.setMapList(mapList);
                        }
                    }

                }else if(type==3){
                    //自己收藏的农情
                    collectList= agrCollectMapper.getMySelfCollectListAgrInfo(userId,pageSize,pageNum);
                    jsonUtil.setCountSum(agrCollectMapper.getMySelfCollectListAgrInfoCountSum(userId));
                    for (AgrCollect agrCollect:collectList){
                        mapList=agrCollectMapper.getListInteract(agrCollect.getId()+"");
                        if(mapList.size()>0){
                            agrCollect.setMapList(mapList);
                        }
                    }
                }


        return    jsonUtil.setData(collectList).setCode(200).setIsSuccess(true);


    }

    @Override
    public JSONUtil delByUseIdAndAfrid(Integer userId, Integer agrId) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("db2");
        Integer count= agrCollectMapper.delByUseIdAndAfrid(userId,agrId);
        if(count>0){
            return  new JSONUtil().setData(true).setMsg("删除成功!");
        }
        return  new JSONUtil().setMsg("删除失败").setCode(100).setData(false);
    }



}
